<?php

    /**
     * @package      Modules
     * @subpackage   Configurations_Classes_Db
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class DbConfiguration extends BaseDbConfiguration
    {

    ////////////////////////////////////////////////////////////////////////////
    //                          Attributes                                    //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Configurations list
         * 
         * @var array
         */
        private static $_configurations = null;

    ////////////////////////////////////////////////////////////////////////////
    //                           Constants                                    //
    ////////////////////////////////////////////////////////////////////////////

        const STAGING_LOCAL = 0;
        const STAGING_DEV   = 1;
        const STAGING_PROD  = 2;

    ////////////////////////////////////////////////////////////////////////////
    //                          Public Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Overrides
         * 
         * @param   Doctrine_Connection  $conn
         */
        public function save(Doctrine_Connection $conn = null)
        {
            if (!$this->DbModule->exists()) {
                // Default module -> configuration
                $this->DbModule = Doctrine_Query::create()
                                    ->from('DbModule module')
                                    ->where('module.slug = ?', 'configurations')
                                    ->fetchOne();
            }

            parent::save($conn);
        }

        /**
         * Overrides
         *
         * @return  string
         */
        public function __toString()
        {
            return $this->constantKey;
        }

        /**
         * Gets a config value
         * 
         * @param   string      $key
         * @param   boolean     $reset
         * @return  mixed|null
         */
        public static function getValue($key, $reset = false)
        {
            if ($reset === true || self::$_configurations === null) {
                $configurations = Doctrine_Query::create()
                                        ->from(__CLASS__ .' config')
                                        ->execute(array(), Doctrine::HYDRATE_ARRAY);

                foreach ($configurations as $configuration) {
                    $value = $configuration['constantValue'];

                    switch ($configuration['type']) {
                        case 'number':
                            $value = floatval($value);
                            break;
                        case 'boolean':
                            $value = ($value == 'true' || $value == 1) ? true : false;
                            break;
                    }

                    self::$_configurations[$configuration['constantKey']] = $value;
                }
            }

            if (array_key_exists($key, self::$_configurations)) {
                return self::$_configurations[$key];
            }

            return null;
        }

        /**
         * Returns true regards on current staging type
         *
         * @return      boolean
         */
        public static function isLocal()
        {
            return self::getValue('STAGING') == self::STAGING_LOCAL;
        }

        /**
         * Returns true regards on current staging type
         *
         * @return      boolean
         */
        public static function isDev()
        {
            return self::getValue('STAGING') == self::STAGING_DEV;
        }

        /**
         * Returns true regards on current staging type
         *
         * @return      boolean
         */
        public static function isProduction()
        {
            return self::getValue('STAGING') == self::STAGING_PROD;
        }
    }